package com.yanjiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yanjiali.feign.ArticleServiceFeign;
import com.yanjiali.model.R;
import com.yanjiali.model.resp.GetAllCollectContentByUserIdResp;
import com.yanjiali.model.resp.GetByIdArticleNoNeedAuthorResp;
import com.yanjiali.model.resp.GetCollectContentBySecondLevelIdResp;
import com.yanjiali.model.resp.SearchCollectByTitleResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjiali.entity.CollectContent;
import com.yanjiali.mapper.CollectContentMapper;
import com.yanjiali.service.CollectContentService;
/**
* @Package: com.yanjiali.service.impl
* @Author: yanjiali
* @Created: 2025/4/19 15:06
*/
@Service
public class CollectContentServiceImpl extends ServiceImpl<CollectContentMapper, CollectContent> implements CollectContentService{

    @Autowired
    private ArticleServiceFeign articleServiceFeign;

    @Override
    public R<Boolean> isTheArticleFavorited(Long articleId) {
        //获取登录用户的id
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        R<GetByIdArticleNoNeedAuthorResp> feignRespR = articleServiceFeign.getByIdArticleNoNeedAuthor(articleId);
        if(feignRespR.getCode() == 200) {
            String title = feignRespR.getData().getTitle();
            LambdaQueryWrapper<CollectContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper
                    .eq(CollectContent::getTitle, title)
                    .eq(CollectContent::getCreateBy, userId);

            CollectContent one = getOne(lambdaQueryWrapper);
            if(one != null) {
                return R.ok(true);
            }
        }

        return R.fail(false);
    }

    @Override
    public R<SearchCollectByTitleResp> searchCollectByTitle(String title) {
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        LambdaQueryWrapper<CollectContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(CollectContent::getCreateBy, userId)
                .like(CollectContent::getTitle, title);

        List<CollectContent> collectContentList = list(lambdaQueryWrapper);

        if(collectContentList == null || collectContentList.size() < 1) {
            return R.fail("没有查找到与之匹配的数据");
        }

        return R.ok(new SearchCollectByTitleResp(collectContentList));
    }

    @Override
    public R deleteASingleCollection(String id) {
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        LambdaUpdateWrapper<CollectContent> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .eq(CollectContent::getCreateBy, userId)
                .eq(CollectContent::getId, id);

        boolean remove = remove(lambdaUpdateWrapper);
        if(remove) {
            return R.ok("删除成功");
        }

        return R.fail("删除失败");
    }

    @Override
    public R deleteAll() {
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        LambdaUpdateWrapper<CollectContent> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .eq(CollectContent::getCreateBy, userId);

        boolean remove = remove(lambdaUpdateWrapper);
        if(remove) {
            return R.ok("清除成功");
        }

        return R.fail("清除失败");
    }

    @Override
    public R<GetCollectContentBySecondLevelIdResp> getCollectContentBySecondLevelId(Long id) {
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        LambdaQueryWrapper<CollectContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(CollectContent::getCreateBy, userId)
                .eq(CollectContent::getSecondLevelCollectId, id);

        List<CollectContent> collectContentList = list(lambdaQueryWrapper);

        if(collectContentList == null || collectContentList.size() < 1) {
            return R.fail("当前收藏夹没有收藏内容");
        }

        return R.ok(new GetCollectContentBySecondLevelIdResp(collectContentList));
    }

    @Override
    public R<GetAllCollectContentByUserIdResp> getAllCollectContentByUserId() {
        String userId = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录");
        }

        LambdaQueryWrapper<CollectContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(CollectContent::getCreateBy, userId);

        List<CollectContent> collectContentList = list(lambdaQueryWrapper);

        if(collectContentList == null || collectContentList.size() < 1) {
            return R.fail("当前收藏夹没有收藏内容");
        }

        return R.ok(new GetAllCollectContentByUserIdResp(collectContentList));
    }
}
